<template>
  <div>
    <el-drawer
      title="我是标题"
      :visible.sync="drawerVisible"
      size="50%"
      direction="rtl"
      :before-close="handleClose"
    >
      <el-tree
        :data="tree"
        show-checkbox
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
        :render-content="renderContent"
      />
    </el-drawer>
    <el-dialog title="报文字段描述" :visible.sync="dialogVisible">
      <el-form :model="form">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in types"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input v-model="form.desc" type="textarea" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: ["tem"],
  data() {
    return {
      tree: [],
      types: this.$types,
      dialogVisible: false,
      form: {
        name: "", //字段名
        type: "", //字段类型
        desc: "" //字段描述
      },
      formLabelWidth: "120px",
      activeData: {},
      drawerVisible: false
    };
  },
  methods: {
    //弹窗确认按钮
    confirm() {
      this.dialogVisible = false;
      this.append();
    },
    //添加节点
    append() {
      const newChild = {
        id: this.$GUID(),
        temID: this.tem.id,
        parentID: this.activeData.id,
        name: this.form.name,
        type: this.form.type,
        desc: this.form.desc,
        children: []
      };
      if (!this.activeData.children) {
        this.$set(this.activeData, "children", []);
      }
      this.$setMBList(newChild);
      this.activeData.children.push(newChild);
    },
    //添加按钮
    add(node, data) {
      this.activeData = data;
      this.dialogVisible = true;
    },
    //删除按钮
    remove(node, data) {
      console.log(data)
      this.$delMBList(data.id)
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex(d => d.id === data.id);
      children.splice(index, 1);
    },
    //修改按钮
    update(node, data) {
      this.activeData = data;
      this.dialogVisible = true;
    },
    //渲染树
    renderContent(h, { node, data, store }) {
      let type = this.getLabel(data.type);
      return (
        <span class="custom-tree-node">
          <span>
            {data.name} | {type}
          </span>
          <span>
            <el-tooltip content={data.desc} placement="bottom" effect="light">
              <el-button size="mini" type="text">
                描述
              </el-button>
            </el-tooltip>
            {data.type == "0" || data.type == "5" ? (
              <el-button
                size="mini"
                type="text"
                on-click={() => this.add(node, data)}
              >
                添加
              </el-button>
            ) : (
              ""
            )}
            <el-button
              size="mini"
              type="text"
              on-click={() => this.update(node, data)}
            >
              修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              on-click={() => this.remove(node, data)}
            >
              删除
            </el-button>
          </span>
        </span>
      );
    },
    handleClose(done) {
      done();
    },
    openDrawer() {
      this.$nextTick(() => {
        this.drawerVisible = true;
        let tree = [];
        let list = this.$getMBList(this.tem.id);
        if (list.length > 0) {
          tree=this.$treeFn(list,"0")
        } else {
          let root = {
            id: this.$GUID(),
            name: "root",
            type: 0,
            desc: "根节点",
            temID: this.tem.id,
            parentID: "0"
          };
          this.$setMBList(root);
          tree.push(root);
        }
        this.tree = tree;
      });
    }
  }
};
</script>